/**
  ******************************************************************************
  * File Name          : stgtsk.c
  * Description        : 存储相关,为参数系统提供底层存储架构和方法
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "parameter.h"
#include <string.h>

TaskHandle_t xStorage_Thd = NULL;
StaticTask_t xStorage_Tcb;
StackType_t  xStorage_Stk[STORAGE_STACK_SIZE];

volatile uint16_t sto_save_feedback;    /* 用于反馈参数保存结果的变量 */

void vStorage_Task(void * pvParameters)
{
  Sto_CmdStru rcmd;
  uint8_t fberr;

  mb85rs64_init();

//  printf("size of Sto_CellTypeEnum %d\r\n", sizeof(Sto_CellTypeEnum));
//  printf("size of Sto_CellUnion %d\r\n", sizeof(Sto_CellUnion));
//  printf("size of Sto_DataStru %d\r\n", sizeof(Sto_DataStru));
//  printf("size of Sto_MaintainStru %d\r\n", sizeof(Sto_MaintainStru));
//  printf("size of Sto_MaintainTableStru %d\r\n", sizeof(Sto_MaintainTableStru));
//  printf("size of Sto_CmdStru %d\r\n", sizeof(Sto_CmdStru));
//  printf("size of Sto_DescribeStrStru %d\r\n", sizeof(Sto_DescribeStrStru));
//  printf("size of sto_maintain_table.maint->upcb %d\r\n", sizeof(sto_maintain_table.maint));
//  printf("size of sto_maintain_table.maint->upcb %d\r\n", sizeof(sto_maintain_table.maint->target));
  
//  sto_init_allitems(&para_reg_table, &gv);
  sto_get_allitems(&sto_maintain_table);

  APP_RunTimeCreateTask();
  
  while(1)
  {
    if(xQueueReceive(storage_qhd, &rcmd, portMAX_DELAY) == pdTRUE)
    {
      switch(rcmd.cmd)
      {
        case STO_CMD_INIT_ITEM:
          sto_init_item(rcmd.idx, &sto_maintain_table, NULL);
          break;
        
        case STO_CMD_GET_ITEM:
          sto_save_feedback = sto_get_item(rcmd.idx, &sto_maintain_table, NULL);
          break;
        
        case STO_CMD_SET_ITEM:
          fberr = sto_set_item(rcmd.idx, &rcmd.dat, &sto_maintain_table, NULL);
          sto_save_feedback = (((uint16_t)fberr) << 8) + rcmd.idx;
          break;
        
        case STO_CMD_INIT_ALLITEMS:
          sto_init_allitems(&sto_maintain_table);
          break;
        
        case STO_CMD_GET_ALLITEMS:
          sto_get_allitems(&sto_maintain_table);
          break;
        
        default:
          break;
      }
    }
  }
}
